<?php

class Ajax extends Controller {

	function get_exchange($date, $v1, $v2)
	{
		global $VALUTA;
		$out = 1;
		if($v1 != $v2)
		{
			if($v1 !== false && $v2 !== false)
			{
				$out = $this->MKurs->getExchange($date, $v1, $v2);
			}
		}
		echo $out;
	}

	function forgot_password()
	{
		$out = '';
		$email = trim($_POST['email']);
		$users = $this->MUser->getList(array('email' => $email));
		if(count($users))
		{
			$user = reset($users);
			$password = substr(md5(microtime()), 0, 8);
			$this->MUser->save(array('password' => md5($password)), $user['id']);
			
			$this->load->library('phpmailer');
			$mail = new PHPMailer(); 
			$mail->IsHTML(true);    
			$mail->Subject = "Восстановление пароля от личного кабинета";
			$mail->Body = "<p>Здравствуйте".($user['fio'] ? ", ".$user['fio'] : '')."!</p><p>&nbsp;</p><p>Вы запросили восстановление пароля от личного кабинета \"Манк\".</p><p>Ваш новый пароль: <span style='color:red; font-size:30px;'><b>".$password."</b></span></p><p><b>ВНИМАНИЕ!<br>Этот пароль - это НЕ ОДНО И ТО ЖЕ с паролем от Личного Кабинета</b></p>";
			$mail->AddAddress($user['email']);
			$mail->Send();

			$out = 'Пароль отправлен на e-mail';
		}
		else
		{
			$out = 'Не найден пользователь с таким e-mail';
		}
		echo $out;
	}

	function get_desyatnik_by_email()
	{
		$root = $this->MUser->getRoot();
		$email = trim($_POST['email']);
		$user = $this->MUser->getFirstRow(array('email' => $email, 'hi_like' => $root['hi'], 'level>' => ''));
		echo @$user['fio'];
	}

	function oper_sort()
	{
		$ids = $_POST['ids'];
		$id_arr = explode(",", $ids);
		$num = 1;
		foreach($id_arr as $id)
		{
			$arr = array('num' => $num);
			$this->MOper->save($arr, $id);
			$num++;
		}
	}

	function get_scheta($user_id = null)
	{
		global $VALUTA;
		$out = array();
		$scheta = $this->MSchet->getList(array('user_id' => $user_id, 'sort' => 'id asc'));
		foreach($scheta as $k => $s)
		{
			$out[] = $s['id']."|".$s['name'].' '.$VALUTA[$s['valuta']];
		}
		echo implode("\n", $out);
	}

	function get_platezhi_buy($date, $from_user_id)
	{
		global $VALUTA;
		$date = dateToDb($date);
		$platezhi = $this->MPlatezh->getMyListByDateBuy($date, $from_user_id);
		foreach($platezhi as $p) 
		{
			platezhFromTo($p);
			echo $p['id']."|".$p['summ']." ".$VALUTA[$p['valuta']]." - ".$p['schet_name']." (".$p['user_from'].")\n";
		}
	}

	function get_platezhi_sell($date, $to_user_id)
	{
		global $VALUTA;
		$date = dateToDb($date);
		$platezhi = $this->MPlatezh->getMyListByDateSell($date, $to_user_id);
		foreach($platezhi as $p) 
		{
			platezhFromTo($p);
			echo $p['id']."|".$p['summ']." ".$VALUTA[$p['valuta']]." - ".$p['schet_name']." (".$p['user_from'].")\n";
		}
	}

	function get_my_user_children($user_id = null, $level = 0)
	{
		$user_id = intval($user_id);
		$level = intval($level);
		if(!$user_id)
		{
			$user_id = Auth::$user_id;
		}
		$me = $this->MUser->get(Auth::$user_id);
		$users = $this->MUser->getList(array('parent_id' => $user_id, 'add_fields' => array('referer_fio'), 'sort' => 'level desc,fio asc'));
		$out = array();
		foreach($users as $u)
		{
			$out[] = userGetToAjax($u, $level);
		}
		echo json_encode($out);
	}

	function get_user_des()
	{
		$field = $_POST['field'];
		if(in_array($field, array('id', 'email', 'fio')))
		{
			$value = trim($_POST['value']);
			$filters = array('level>=' => '*');
			if($field == 'id')
			{
				$filters['id'] = intval($value);
			}
			if($field == 'fio')
			{
				$filters['fio_search'] = $value;
			}
			if($field == 'email')
			{
				$filters['email_search'] = $value;
			}
			$user = $this->MUser->getFirstRow($filters);
			if(@$user['id'])
			{
				echo $user['id']."/".$user['fio'].'/'.$user['email'];
			}
		}
	}

}